﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDTB.Service;

namespace LDTB
{
    public partial class VillageManger : Form
    {

        LDTBDataDataContext ds = DataService.DB;

        public VillageManger()
        {
            InitializeComponent();
        }


        

        private void VillageManger_Load(object sender, EventArgs e)
        {

            List<Ward> list = new List<Ward>();
            list.Add(new Ward { ID = -1, WardName = "Tất cả" });

            list.AddRange(from w in ds.Wards select w);

            ddlWard.DataSource = list;
            ddlWard.DisplayMember = "WardName";
            ddlWard.ValueMember = "ID";


            //load du lieu vao grid view

FillDataToGrid(-1);

        }

        public  void  FillDataToGrid(int warid)
        {


            var data = from d in ds.Villages
                       where d.WardID==warid || warid==-1
                       select new { VillageID = d.VillageID, VillageName = d.VillageName };

            this.dataGridView1.DataSource = data;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            VillageAdd va = new VillageAdd();
            va.ShowDialog();

        }

        private void button2_Click(object sender, EventArgs e)
        {

            int cityid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

            VillageEdit ve = new VillageEdit(cityid);
            ve.ShowDialog();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ddlWard_SelectionChangeCommitted(object sender, EventArgs e)
        {
            int wardid = (int)ddlWard.SelectedValue;

            FillDataToGrid(wardid);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int cityid = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());

              DialogResult dr = MessageBox.Show(this,
                                              "Nếu xã này  thì danh sách người tàn tật thuộc xã này sẽ bị xóa  bạn có chắc chắn xóa không ? ",
                                              "Xóa xã ", MessageBoxButtons.YesNo);

              if (dr == DialogResult.Yes)
              {
                  var pl = from p in ds.Peoples
                           where p.LocationID == cityid 
                           select  p;

                  ds.Peoples.DeleteAllOnSubmit(pl);
                  ds.SubmitChanges();

                  var village = from vil in ds.Villages
                                where vil.VillageID == cityid 
                                select vil;

                  ds.Villages.DeleteAllOnSubmit(village);
                  ds.SubmitChanges();

                  FillDataToGrid(-1);
              }
        }

    }
}
